package com.pn.filter;

import com.alibaba.fastjson.JSON;
import com.pn.entity.Result;
import com.pn.utils.WarehouseConstants;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author :LY
 * @Description :
 * @Date :2025/3/19 22:23
 */
public class LoginCheckFilter implements Filter {

    private StringRedisTemplate redisTemplate;

    public void setRedisTemplate(StringRedisTemplate redisTemplate){
        this.redisTemplate = redisTemplate;
    }


    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        /*
          1、白名单请求直接放行
         */
        //创建List<String>放白名单url接口
        List<String> urlList = new ArrayList<>();
        urlList.add("/captcha/captchaImage");
        urlList.add("/login");
        urlList.add("/logout");
        urlList.add("/product/img-upload");
        //过滤器拦截到的当前请求的url接口
        String url = request.getServletPath();
        if(urlList.contains(url)||url.contains("/img/upload")){//白名单请求
            filterChain.doFilter(request,response);
            return;
        }

        /*
            2、其他请求校验是否携带token，以及判断redis中是否存在token的键
         */
        String token = request.getHeader(WarehouseConstants.HEADER_TOKEN_NAME);
        //1>有，说明已登录，请求放行
            if(StringUtils.hasText(token)&&redisTemplate.hasKey(token)){
                filterChain.doFilter(request,response);
                return;
            }
        //2>没有，说明未登录或token过期，请求不放行，并给前端作出响应
       Result result =  Result.err(Result.CODE_ERR_UNLOGINED,"您尚未登录！");
       String jsonStr = JSON.toJSONString(result);
       response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(jsonStr);
        out.flush();
        out.close();
    }

}
